﻿namespace PMS.Client.FrmOrgan
{
    using FastReport;
    using FastReport.Preview;
    using FastReport.Utils;
    using PMS.BLL;
    using PMS.Common;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class FrmPositionConfigReport : Form
    {
        private OrganConfigBLL bll = new OrganConfigBLL();
        private Button btnPrint;
        private string businessPath = (Application.StartupPath + @"\ReportTemplates\positionConfig.frx");
        private IContainer components = null;
        private Label label1;
        private OrganConfig model = null;
        private string officePath = (Application.StartupPath + @"\ReportTemplates\officeConfig.frx");
        private OrganBLL ogranBLL = new OrganBLL();
        private Organ organ = null;
        private int OrganID = 0;
        private string organName = "";
        private Panel panel1;
        private PreviewControl previewControl1;
        private FastReport.Report report1 = null;

        public FrmPositionConfigReport(int OrganID, string organName)
        {
            this.OrganID = OrganID;
            this.organName = organName;
            this.organ = this.ogranBLL.GetModel(OrganID);
            this.InitializeComponent();
            this.BindReport();
        }

        private void BindReport()
        {
            try
            {
                this.model = this.bll.GetModel(this.OrganID);
                if (this.model == null)
                {
                    this.model = new OrganConfig();
                }
                this.report1 = new FastReport.Report();
                if (this.organ.IsOffice == 2)
                {
                    this.report1.Load(this.businessPath);
                }
                else
                {
                    this.report1.Load(this.officePath);
                }
                this.report1.Preview = this.previewControl1;
                this.report1.Parameters.FindByName("OrganName").Value = this.organName;
                this.report1.RegisterData(new List<OrganConfig> { this.model }, "OrganReport");
                this.report1.Show();
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                if (MessageBox.Show("报表加载失败，是否重新加载？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                {
                    this.BindReport();
                }
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                this.report1.Print();
            }
            catch (Exception exception)
            {
                Log.Write(exception.Message);
                MessageBox.Show("未安装打印机或打印服务未开启！");
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.previewControl1 = new PreviewControl();
            this.panel1 = new Panel();
            this.btnPrint = new Button();
            this.label1 = new Label();
            this.panel1.SuspendLayout();
            base.SuspendLayout();
            this.previewControl1.BackColor = SystemColors.Control;
            this.previewControl1.Dock = DockStyle.Fill;
            this.previewControl1.Font = new Font("宋体", 9f);
            this.previewControl1.Location = new Point(0, 0);
            this.previewControl1.Margin = new Padding(0);
            this.previewControl1.Name = "previewControl1";
            this.previewControl1.PageOffset = new Point(0, 0);
            this.previewControl1.Size = new Size(950, 0x1b5);
            this.previewControl1.StatusbarVisible = false;
            this.previewControl1.TabIndex = 0;
            this.previewControl1.ToolbarVisible = false;
            this.previewControl1.UIStyle = UIStyle.VisualStudio2005;
            this.panel1.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            this.panel1.Controls.Add(this.btnPrint);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Dock = DockStyle.Bottom;
            this.panel1.Location = new Point(0, 0x1b5);
            this.panel1.Name = "panel1";
            this.panel1.Padding = new Padding(0, 3, 0, 3);
            this.panel1.Size = new Size(950, 30);
            this.panel1.TabIndex = 1;
            this.btnPrint.BackColor = Color.White;
            this.btnPrint.Dock = DockStyle.Right;
            this.btnPrint.FlatStyle = FlatStyle.Flat;
            this.btnPrint.Location = new Point(0x35a, 3);
            this.btnPrint.Margin = new Padding(0);
            this.btnPrint.Name = "btnPrint";
            this.btnPrint.Size = new Size(0x4b, 0x18);
            this.btnPrint.TabIndex = 0;
            this.btnPrint.Text = "打印";
            this.btnPrint.UseVisualStyleBackColor = false;
            this.btnPrint.Click += new EventHandler(this.btnPrint_Click);
            this.label1.Dock = DockStyle.Right;
            this.label1.Location = new Point(0x3a5, 3);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x11, 0x18);
            this.label1.TabIndex = 1;
            this.label1.Text = "  ";
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(950, 0x1d3);
            base.Controls.Add(this.previewControl1);
            base.Controls.Add(this.panel1);
            base.Name = "FrmPositionConfigReport";
            this.Text = "FrmReport";
            this.panel1.ResumeLayout(false);
            base.ResumeLayout(false);
        }
    }
}

